#region Using Directives

using System;
using System.IO;
using System.Windows.Forms;
using System.Globalization;
using Microsoft.VisualStudio.Modeling;
using Microsoft.Practices.ComponentModel;
using Microsoft.Practices.RecipeFramework;
using Microsoft.Practices.RecipeFramework.Library;
using Microsoft.Practices.RecipeFramework.Services;
using Microsoft.Practices.RecipeFramework.VisualStudio;
using Microsoft.Practices.RecipeFramework.VisualStudio.Templates;
using EnvDTE;
using EastBancTech.InstallerDefinition;
using Microsoft.Practices.Modeling.Dsl.Integration.Helpers;
using WixPackageDsl.Helper;
using FileActionHelpers = EBT.Deployment.SoftwareFactory.FileActions.Helpers;

#endregion

namespace EBT.Deployment.SoftwareFactory.Actions
{
    public class AddFileActionToModelAction: ActionBase
    {
        String _name;
        [Input(Required = true)]
        public String Name
        {
            get { return _name; }
            set { _name = value; }
        }

        ComponentFileSource _element;
        [Input(Required = true)]
        public Object Element
        {
            get { return _element; }
            set { _element = (ComponentFileSource)value; }
        }

        Store _wixPackageStore;
        [Input(Required = true)]
        public Store WixPackageStore
        {
            get { return _wixPackageStore; }
            set {_wixPackageStore = value; }
        }        

        DomainClassInfo _type;
        [Input(Required = true)]
        public Object Type
        {
            get { return _type; }
            set { _type = (DomainClassInfo)value; }
        }

        ModelElement[] _values;
        [Input(Required = true)]
        public Array Values
        {
            get { return _values; }
            set 
            { 
                _values = (ModelElement[])value; 
            }
        }

        FileSet _fileSet;
        [Input(Required = false)]
        public Object FileSet
        {
            get { return _fileSet; }
            set { _fileSet = (FileSet)value; }
        }

        
        
        protected override void OnExecute()
        {
            
            InstallerPackageModel cmpPackage = DomainModelHelper.GetElement<InstallerPackageModel>(WixPackageStore) as InstallerPackageModel;

            if (cmpPackage == null)
                throw new ApplicationException("Current DSL model does not contain 'InstallerPackageModel'");

            using (Transaction transaction = WixPackageStore.TransactionManager.BeginTransaction("Add new IIS Host and Pool"))
            {               
                                                
                FileAction fileAction = null;                
                fileAction = (FileAction)WixPackageStore.ElementFactory.CreateElement(this._type.Id);
                
                fileAction.Name = _name;
                DomainModelHelper.AddLinkedElement(cmpPackage.Definitions.FileActions, fileAction);
                _element.FileActions.Add(fileAction);
                if (_fileSet != null)
                    fileAction.FileSet = _fileSet;
                FileActionHelpers::FileActionHelper _helper =
                    FileActionHelpers::FileActionHelper.CreateInstance(_type);
                _helper.AddValuesToFileAction(_wixPackageStore, fileAction, _values);
                transaction.Commit();
            }
             
        }
    }
}
